﻿using ACC.Data.Entities.Common;
using ACC.Extensions.ClientBusiness;
using ACC.Extensions.Entities;
using ACC.Extensions.PCClient.Helper;
using System;
using System.Collections.Generic;
using System.Windows;

namespace ACC.Extensions.PCClient.View
{
    public enum RetryType
    {
        Load,
        Unload
    }

    /// <summary>
    /// Interaction logic for LoadUnloadRetryWnd.xaml
    /// </summary>
    public partial class LoadUnloadRetryWnd
    {
        private RetryType _retryType;
        private List<string> _barcodes;
        private AccProcess _accProcess;
        private List<ClientProcData> _processDatas;
        private int _statusBit;

        public LoadUnloadRetryWnd(RetryType retryType, List<string> barcodes, AccProcess accProcess, int statusBit, string msg, List<ClientProcData> processDatas = null)
        {
            InitializeComponent();
            this.Owner = Application.Current.MainWindow;
            _retryType = retryType;
            _barcodes = barcodes;
            _accProcess = accProcess;
            _processDatas = processDatas;

            if (retryType == RetryType.Load)
            {
                txtErrorMessage.Text = String.Format("{0}:{1}\r\n", LanguageHelper.LoadStringFromResource("MSG_ACC_LOAD_FAILD"), msg);
            }
            else
            {
                txtErrorMessage.Text = LanguageHelper.LoadStringFromResource("MSG_ERR_UNLOAD_RETRY") + "\r\n";
            }
        }

        /// <summary>
        /// 向ACC重发条码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRetry_Click(object sender, RoutedEventArgs e)
        {
            switch (_retryType)
            {
                case RetryType.Load:
                    var loadResult = _accProcess.Load(_barcodes, UISession.CurrentPartType == null ? 0 : UISession.CurrentPartType.PartTypeId);
                    if (loadResult.Result)
                    {
                        this.DialogResult = true;
                        this.Close();
                    }
                    else
                    {
                        txtErrorMessage.Text += String.Format("{0}:{1}\r\n",
                            LanguageHelper.LoadStringFromResource("MSG_ACC_LOAD_FAILD"), loadResult.Message);
                    }
                    break;
                case RetryType.Unload:
                    var unloadResult = _accProcess.Unload(_barcodes, _processDatas, null, _statusBit,0, UISession.CurrentPartType == null ? 0 : UISession.CurrentPartType.PartTypeId);
                    if (unloadResult.Result)
                    {
                        this.DialogResult = true;
                        this.Close();
                    }
                    else
                    {
                        txtErrorMessage.Text += String.Format("{0}:{1}\r\n",
                            LanguageHelper.LoadStringFromResource("MSG_ACC_UNLOAD_FAILD"), unloadResult.Message);
                    }
                    break;
                default:
                    break;
            }
        }

        private void MetroWindow_Activated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }

        private void MetroWindow_Deactivated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }
    }
}
